using Microsoft.AspNetCore.SignalR;
namespace Magic.System;
/// <summary>
/// 消息发送
/// </summary>
[Injection]
public class MessageService : IMessageService
{
    private readonly IHubContext<ChatHub, IChatHub> _chatHub;
    public MessageService(IHubContext<ChatHub, IChatHub> chatHub)
    {
        _chatHub = chatHub;
    }
    /// <summary>
    /// 发送消息给所有人
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task SendAllUser(MessageInput input)
    {
        await _chatHub.Clients.All.SendMessage(input);
    }
    /// <summary>
    /// 发送消息给除了发送人的其他人
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task SendOtherUser(MessageInput input)
    {
        var user=await SugarContext.Instance.Queryable<SysUserOnline>().FirstAsync(p=>p.UserId==input.UserIds.First());
        await _chatHub.Clients.AllExcept(user.ConnectionId).SendMessage(input);
    }
    /// <summary>
    /// 发送消息给某个人
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task SendUser(MessageInput input)
    {
        var user=await SugarContext.Instance.Queryable<SysUserOnline>().FirstAsync(p=>p.UserId==input.UserIds.First());
        await _chatHub.Clients.Client(user.ConnectionId).SendMessage(input);
    }
    /// <summary>
    /// 发送消息给某些人
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task SendUsers(MessageInput input)
    {
        var users=await SugarContext.Instance.Queryable<SysUserOnline>().Where(p=>input.UserIds.Contains(p.UserId)).ToListAsync();
        await _chatHub.Clients.Clients(users.Select(p=>p.ConnectionId)).SendMessage(input);
    }
}
